package cn.gson.crm.controller.employee;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.dao.EmpDao;
import cn.gson.crm.model.dao.PositionDao;
import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.entity.Position;

/**
 * 员工修改
 */
@WebServlet("/employee/revise")
public class EmployeeReviseServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private EmpDao empDao = new EmpDao();
	private DeptDao deptDao = new DeptDao();
	private PositionDao postDao = new PositionDao();
	String formJsp = "employee/emp_revise.jsp";
	String id = "";
	List<Dept> deptList = null;
	List<Position> postList = null;
	String yuanPassword = "";

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		id = request.getParameter("id");
		Employee employee = empDao.getUserById(id);
		yuanPassword = employee.getEmpPassword();
		deptList = deptDao.getAllDept();
		postList = postDao.getPostByStatus(1);

		turn(request, deptList, postList, employee);
		forward("employee/emp_revise.jsp");
	}

	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String gender = request.getParameter("gender");
		String post = request.getParameter("post").toString();
		String dept = request.getParameter("dept");
		String tel = request.getParameter("tel");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		Employee emp = new Employee();
		emp.setEmpName(name);
		if (post != "" && post != null) {
			emp.setPostId(Integer.parseInt(post));
		}
		if (dept != "" && dept != null) {
			emp.setDeptId(Integer.parseInt(dept));
		}
		if (password == null || password.isEmpty()) {
			password = yuanPassword;
			password = addPass(password);
		}
		emp.setEmpPassword(password);
		emp.setEmpTelphone(tel);
		emp.setEmpEmail(email);
		emp.setEmpAddress(address);
		emp.setEmpGender(gender);
		// 验证
		if (name == null || name.isEmpty()) {
			turn(request, deptList, postList, emp);
			errorForward("姓名不能为空！", formJsp);
			return;
		} else {
			Pattern pattern = Pattern.compile("[\u4E00-\u9FA5]{2,4}");
			Matcher mqq = pattern.matcher(name);
			boolean bqq = mqq.matches();
			if (!bqq) {
				turn(request, deptList, postList, emp);
				errorForward("员工姓名格式不对", formJsp);
				return;
			}
		}

		// 验证
		if (gender == null || gender.isEmpty()) {
			turn(request, deptList, postList, emp);
			errorForward("性别不能为空！", formJsp);
			return;
		}
		// 验证
		if (dept == null || dept.isEmpty()) {
			turn(request, deptList, postList, emp);
			errorForward("部门不能为空！", formJsp);
			return;
		}

		// 验证
		if (post == null || post.isEmpty()) {
			turn(request, deptList, postList, emp);
			errorForward("职位不能为空！", formJsp);
			return;
		}

		// 验证电话
		// 验证
		if (tel != null && !tel.isEmpty()) {
			// 判断手机号
			Pattern patternTell = Pattern.compile("^1[0-9]\\d{9}$");
			Matcher mTell = patternTell.matcher(tel);
			boolean bTell = mTell.matches();
			if (!bTell) {
				turn(request, deptList, postList, emp);
				errorForward("电话格式错误！", formJsp);
				return;
			}
		}
		// 验证邮箱
		if (email != null && !email.isEmpty()) {
			Pattern pmail = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");
			Matcher mmail = pmail.matcher(email);
			boolean bmail = mmail.matches();
			if (!bmail) {
				turn(request, deptList, postList, emp);
				errorForward("邮箱格式错误！", formJsp);
				return;
			}
		}

		boolean result = empDao.updateEmp(emp, id);
		if (result) {
			redirect("employee");
		} else {
			turn(request, deptList, postList, emp);
			errorForward("更新失败", "employee/form");
		}
	}

	private void turn(HttpServletRequest request, List<Dept> deptList, List<Position> postList, Employee emp) {
		request.setAttribute("employee", emp);
		request.setAttribute("postList", postList);
		request.setAttribute("deptList", deptList);
	}
}
